﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;
namespace AutoChess
{
    /// <summary>
    /// 非loading 阶段的重新连接的时候的资源加载
    /// </summary>
     class ACReconnectState : Yunchang.FsmStateBase<AutoChessModeState>
    {
        private ResLoadState curState = ResLoadState.None;
        private AutoChessManager ctr;

        public ACReconnectState(AutoChessModeState stateType, AutoChessManager controller) : base(stateType, controller)
        {
            ctr = controller;
        }
        public override void Enter()
        {
            base.Enter();
            if (curState == ResLoadState.None)
                curState = ResLoadState.Init;
            //ask data
            Yunchang.EventManager.TriggerEvent<string, bool, float>(CommonEvent.ShowLoading, "", false, 3f);
            AssetManager.Instance.LoadAsset(GameConst.SceneConfig);
        }
        public override void Leave()
        {
            base.Leave();
        }
       
        private double lastProgress = 0;
        public override void Update()
        {
            base.Update();
            if (curState == ResLoadState.Init)
            {
                AutoChessManager.Ins.SceneCmpt.LoadCurPlayerScene();
                curState = ResLoadState.LoadScene_Ship_ing;
            }
            else if (curState == ResLoadState.LoadScene_Ship_ing)
            {
                if (AutoChessManager.Ins.SceneCmpt.IsSceneLoadComplete)
                {
                    curState = ResLoadState.LoadSceneConfig;
                }
            }
            else if (curState == ResLoadState.LoadSceneConfig)
            {
                if (AssetManager.Instance.IsAssetLoaded(GameConst.SceneConfig))
                {
                    var asset = AssetManager.Instance.GetAsset(GameConst.SceneConfig);
                    AutoChessFieldViewCtr.Ins.OnLoadedSceneConfig(asset);
                    curState = ResLoadState.LoadEntity;
                }
            }
            else if (curState == ResLoadState.LoadEntity)
            {
                ctr.InitGameFieldUnit();
                curState = ResLoadState.Complete;
            }
            else if (curState == ResLoadState.Complete)
            {
                curState = ResLoadState.None;
                OnAllPlayerLoaded();
            }
        }

        /// <summary>
        /// 服务器通知可以进入了
        /// </summary>
        private void OnAllPlayerLoaded()
        {
            Debug.Log("<color=yellow> 重连成功可以进入自走棋了 </color>");
            AutoChessManager.Ins.SceneCmpt.PlayCurSceneAudio();

            PageManager.Instance.RestoreHistory();
            PageManager.Instance.OpenPage("AutoChessMainPage");
            EmojiManager.Instance.LoadEmojiBundle();

            AutoChessCamera.Ins.SwitchCamera(true);
            SceneUIManager.Instance.SetCamera(SceneUICameraType.ThreeD, AutoChessCamera.Ins.acCamera);

            EventManager.TriggerEvent(CommonEvent.HideLoading);

            ctr.SwitchState(AutoChessModeState.GameFieldtate);
            ctr.IsEnterSceneInit = true;
        }
    }
}
